package com.ruoyi.spider.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.spider.domain.SpiderTask;
import com.ruoyi.spider.service.ISpiderTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 爬虫任务Controller
 * 
 * @author 小光头
 * @date 2021-01-15
 */
@Controller
@RequestMapping("/spider/spiderTask")
public class SpiderTaskController extends BaseController
{
    private String prefix = "spider/spiderTask";

    @Autowired
    private ISpiderTaskService spiderTaskService;



    /**
     * 启动爬虫任务
     */
    @PostMapping( "/runSpiderTask/{taskId}")
    @ResponseBody
    public AjaxResult runSpiderTask(@PathVariable("taskId") String taskId)
    {
       spiderTaskService.runSpiderTask(taskId);
        return null;
    }

    @RequiresPermissions("spider:spiderTask:view")
    @GetMapping()
    public String spiderTask()
    {
        return prefix + "/spiderTask";
    }

    /**
     * 查询爬虫任务列表
     */
    @RequiresPermissions("spider:spiderTask:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SpiderTask spiderTask)
    {
        startPage();
        List<SpiderTask> list = spiderTaskService.selectSpiderTaskList(spiderTask);
        return getDataTable(list);
    }

    /**
     * 导出爬虫任务列表
     */
    @RequiresPermissions("spider:spiderTask:export")
    @Log(title = "爬虫任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SpiderTask spiderTask)
    {
        List<SpiderTask> list = spiderTaskService.selectSpiderTaskList(spiderTask);
        ExcelUtil<SpiderTask> util = new ExcelUtil<SpiderTask>(SpiderTask.class);
        return util.exportExcel(list, "spiderTask");
    }

    /**
     * 新增爬虫任务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存爬虫任务
     */
    @RequiresPermissions("spider:spiderTask:add")
    @Log(title = "爬虫任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SpiderTask spiderTask)
    {
        return toAjax(spiderTaskService.insertSpiderTask(spiderTask));
    }

    /**
     * 修改爬虫任务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        SpiderTask spiderTask = spiderTaskService.selectSpiderTaskById(id);
        mmap.put("spiderTask", spiderTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存爬虫任务
     */
    @RequiresPermissions("spider:spiderTask:edit")
    @Log(title = "爬虫任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SpiderTask spiderTask)
    {
        return toAjax(spiderTaskService.updateSpiderTask(spiderTask));
    }

    /**
     * 删除爬虫任务
     */
    @RequiresPermissions("spider:spiderTask:remove")
    @Log(title = "爬虫任务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(spiderTaskService.deleteSpiderTaskByIds(ids));
    }
}
